﻿namespace LeetCodeProject._0001_1000._101_200._101_110._108;

public class Solution
{
    public TreeNode SortedArrayToBST(int[] nums)
    {
        return SortedArray(nums.AsSpan());
    }

    private TreeNode SortedArray(Span<int> span)
    {
        return span.Length == 0
            ? null
            : new TreeNode(span[span.Length / 2])
            {
                left = SortedArray(span.Slice(0, span.Length / 2)),
                right = SortedArray(span.Slice(span.Length / 2 + 1))
            };
    }
}